package com.example.textjindu;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.ProgressBar;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private ProgressBar progressBar1;
    private int mprogress = 0;
    private Handler mHandler;//消息处理对象

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);//设置全屏显示


        progressBar1 = (ProgressBar)findViewById(R.id.progressBar1);
        mHandler = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                    if (msg.what==0x111){
                        progressBar1.setProgress(mprogress);
                    }else{
                        Toast.makeText(MainActivity.this, "加载完毕", Toast.LENGTH_SHORT).show();
                        progressBar1.setVisibility(View.GONE);//设置进度条不显示
                    }
            }
        };

    new Thread(new Runnable() {
        @Override
        public void run() {
            while(true){
                mprogress=doWork();
                Message m=new Message();
                if(mprogress<100){
                    m.what=0x111;//自定义的消息代码,通过它可以区分消息进行不同的处理，0x111表示耗时操作未完成
                    mHandler.sendMessage(m);
                }else {
                    m.what=0x110;//0x110表示当前的操作已经完成
                    mHandler.sendMessage(m);
                    break;
                }
            }
        }
        private int doWork(){
            mprogress+=Math.random()*10;
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return mprogress;
        }
    }).start();//进行开启线程
    }
}
